/*
 * @描述: 消息通知数据请求
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
 */
import { queryNotification, deleteNotification, addNotification, updateNotification, queryNotificationType, queryNotificationPriority } from '@/services/api';

export default {
  namespace: 'notification',

  state: {
    data: {
      list: [],
      pagination: {}
    },
    typeSelectData: [],
    prioritySelectData: []
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryNotification, payload);
      yield put({
        type: 'save',
        payload: response
      });
    },
    *fetchType({ payload }, { call, put }) {
      const response = yield call(queryNotificationType, payload);
      yield put({
        type: 'saveType',
        payload: response
      });
    },
    *fetchPriority({ payload }, { call, put }) {
      const response = yield call(queryNotificationPriority, payload);
      yield put({
        type: 'savePriority',
        payload: response
      });
    },
    *add({ payload }, { call, put }) {
      const response = yield call(addNotification, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *delete({ payload }, { call, put }) {
      const response = yield call(deleteNotification, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    },
    *update({ payload }, { call, put }) {
      const response = yield call(updateNotification, payload);
      yield put({
        type: 'save',
        payload: response
      });
      if (!response.message) return;
      return response;
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data
      };
    },
    saveType(state, action) {
      return {
        ...state,
        typeSelectData: action.payload.data
      };
    },
    savePriority(state, action) {
      return {
        ...state,
        prioritySelectData: action.payload.data
      };
    }
  }
};
